package cn.cvs.controller;

import cn.cvs.pojo.SysUser;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.sql.Date;
import java.util.ArrayList;

/**
 * @Package: cn.cvs.controller
 * @ClassName: SysUserController
 * @Author: 862982795
 * @CreateTime: 2021/07/15 14:29
 * @Description:
 */
@Controller
@RequestMapping(path = "/user")
public class SysUserController {
    private final Logger logger = Logger.getLogger(SysUserController.class);
    private ArrayList<SysUser> sysUserList = new ArrayList<SysUser>();
    // 用于返回查询到的用户数据
    private ArrayList<SysUser> queryUserList = new ArrayList<SysUser>();

    public SysUserController() {
        try {
            sysUserList.add(new SysUser(5, "zhaojing", "赵静", "5555", 1, "13054784445", "上海市宝山区", 1, 1, new Date(0), 1, new Date(0)));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 查询所有用户
    @GetMapping("/list")
    public String list(Model model) {
        logger.info("当查询条件为空时，查询用户信息");
        queryUserList.clear();
        queryUserList.addAll(sysUserList);
        model.addAttribute("queryUserList", queryUserList);
        return "sysUser/sysUserList";
    }


    public String list(@RequestParam(value = "realName", required = false) String realName, Model model) {
        logger.info("查询条件:realName = " + realName + ",查询用户信息");
        queryUserList.clear();
        if (realName != null && !realName.equals("")) {
            for (SysUser user : sysUserList) {
                if (user.getRealName().indexOf(realName) != -1) {
                    queryUserList.add(user);
                }
            }
        } else {
            queryUserList.addAll(sysUserList);
        }
        model.addAttribute("queryUserList", queryUserList);
        return "sysUser/sysUserList";
    }

}
